package com.cssw.fyzb.busi.util;

import com.cssw.fyzb.core.util.StringUtil;
import org.gavaghan.geodesy.Ellipsoid;
import org.gavaghan.geodesy.GeodeticCalculator;
import org.gavaghan.geodesy.GlobalCoordinates;

/**
 * @author Feng Chen
 */
public abstract class GeoUtil {

    private GeoUtil() {

    }

    /**
     * 获取维度
     *
     * @param loc
     * @return
     */
    public static Double getLat(String loc) {
        if (StringUtil.isEmpty(loc)) {
            return null;
        }
        return Double.valueOf(loc.split(",")[0]);
    }

    /**
     * 获取经度
     *
     * @param loc
     * @return
     */
    public static Double getLng(String loc) {
        if (StringUtil.isEmpty(loc)) {
            return null;
        }
        return Double.valueOf(loc.split(",")[1]);
    }

    public static String distance(double latSrc, double lngSrc, double latDst, double lngDst) {
        double meter = new GeodeticCalculator()
                .calculateGeodeticCurve(
                        Ellipsoid.Sphere,
                        new GlobalCoordinates(latSrc, lngSrc),
                        new GlobalCoordinates(latDst, lngDst)
                ).getEllipsoidalDistance();

        return String.format("%.2f", meter);
    }

    public static void main(String[] args) {
        String distance1 = GeoUtil.distance(28.185454, 113.032007, 28.129153, 113.034753);
        String distance2 = GeoUtil.distance(28.129153, 113.034753, 28.185454, 113.032007);
        System.out.println(distance1);
        System.out.println(distance2);
    }

}
